﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace InfoPick.Lib
{
    public class EGAnalysis
    {
        public static List<EGInfo> Analysis(List<EGInfo> egList, List<MSCInfo> msciList, List<MSCInfo> msciTxtList, int time1, int time2, string egFileName)
        {
            int intTime = time1;
            int intTime2 = time2;
            List<EGInfo> egIdList = new List<EGInfo>();
            List<EGInfo> egRegList = new List<EGInfo>();
            List<EGInfo> egCnfList = new List<EGInfo>();
            List<EGInfo> returnList = new List<EGInfo>();
            int count = egList.Count;
            for (int i = 0; i < count; i++)
            {
                if (egList[i].MessageType == null)
                {
                    break;
                }
                if (egList[i].MessageType.Contains("MAP_U_ABORT_REQ"))
                {
                    egIdList.Add(egList[i]);/////IeDialogueId列表
                }
                if (egList[i].MessageType.Contains("MAP_PREPARE_HANDOVER_REQ"))
                {
                    egRegList.Add(egList[i]);
                }
                if (egList[i].MessageType.Contains("MAP_PREPARE_HANDOVER_CNF"))
                {
                    egCnfList.Add(egList[i]);
                }
            }
            /////根据IeDialogueId,找到time内的MAP_PREPARE_HANDOVER_REQ消息
            if (egIdList.Count > 0)
            {
                for (int i = 0; i < egIdList.Count; i++)
                {
                    int ie_id = egIdList[i].IeDialogueId;
                    for (int j = 0; j < egRegList.Count; j++)
                    {
                        int ie_reg = egRegList[j].IeDialogueId;
                        if (ie_id == ie_reg)
                        {
                            TimeSpan timespan = egIdList[i].Time - egRegList[j].Time;
                            int tmp = timespan.Seconds;
                            if (tmp >= 0 && tmp < intTime)
                            {
                                egIdList[i].OrigDistrict = egRegList[j].OrigDistrict;
                                egIdList[i].TargDistrict = egRegList[j].TargDistrict;
                                egIdList[i].Count = 1;
                                //int reCount = returnList.Count;
                                //if (reCount > 0)
                                //{
                                //    int countNumber = 0;
                                //    for (int k = 0; k < reCount; k++)
                                //    {
                                //        if (returnList[k].OrigDistrict == egRegList[j].OrigDistrict &&
                                //            returnList[k].TargDistrict == egRegList[j].TargDistrict)
                                //        {
                                //            returnList[k].Count++;
                                //        }
                                //        else
                                //        {
                                //            countNumber++;
                                //        }
                                //        if (countNumber == reCount)///判断在returnlist中没有就插入
                                //        {
                                //            returnList.Add(egIdList[i]);
                                //            break;
                                //        }
                                //    }
                                //}
                                //else
                                {
                                    returnList.Add(egIdList[i]);
                                }
                                
                            }
                        }
                    }
                }
                /////根据IeDialogueId，在MAP_PREPARE_HANDOVER_CNF消息中找到10位TargServerId 在搜索时间范围内
                if (returnList.Count > 0)
                {
                    for (int i = 0; i < returnList.Count; i++)
                    {
                        for (int j = 0; j < egCnfList.Count; j++)
                        {
                            if (returnList[i].IeDialogueId == egCnfList[j].IeDialogueId)
                            {
                                TimeSpan timespan = returnList[i].Time - egCnfList[j].Time;
                                int tmp = timespan.Seconds;
                                if (tmp >= 0 && tmp < intTime2)
                                {
                                    returnList[i].TargServerId = egCnfList[j].TargServerId;
                                    returnList[i].invalid_cell = egCnfList[j].invalid_cell;
                                    break;
                                }
                            }
                        }
                    }
                }
                
                if (returnList.Count > 0)
                {
                    //var q = from p in returnList group p by new { p.OrigDistrict, p.TargDistrict } into g select new { g.Key, Count = g.Count() };
                    var grp = returnList.GroupBy(c => new { c.OrigDistrict, c.TargDistrict });
                    returnList = grp.Select(c => {
                        c.First().Count = c.Count();
                        return c.First();
                    }).ToList();
                }

                if (returnList.Count > 0)
                {
                    for (int i = 0; i < returnList.Count; i++)
                    {
                        if (returnList[i].TargServerId == null && returnList[i].invalid_cell == false)
                        {
                            returnList.RemoveAt(i);
                            i--;
                        }
                    }
                }
                /////根据TargServerId找到mscid和gridName
                if (returnList.Count > 0)
                {
                    for (int i = 0; i < returnList.Count; i++)
                    {
                        for (int k = 0; k < msciList.Count; k++)
                        {
                            if (returnList[i].TargServerId != null)
                            {
                                if (returnList[i].TargServerId.Contains(msciList[k].MSCID))
                                {
                                    returnList[i].TargDistrictName = msciList[k].GridName;
                                    break;
                                }
                                else
                                {
                                    returnList[i].TargDistrictName = "未知";
                                }
                            }                  
                        }
                        if (msciTxtList != null)
                        {
                            if (msciTxtList.Count > 0 && returnList[i].TargDistrictName == "未知")
                            {
                                for (int l = 0; l < msciTxtList.Count; l++)
                                {
                                    if (returnList[i].TargServerId == msciTxtList[l].MSCID)
                                    {
                                        returnList[i].TargDistrictName = msciTxtList[l].Name;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                //根据打开文件名,找到对应的mscid和gridName
                if (returnList.Count > 0)
                {
                    for (int i = 0; i < returnList.Count; i++)
                    {
                        for (int k = 0; k < msciList.Count; k++)
                        {
                            if (egFileName.Contains(msciList[k].GridName))
                            {
                                returnList[i].OrigDistrictId = msciList[k].MSCID;
                                returnList[i].OrigDistrictName = msciList[k].GridName;
                                break;
                            }
                        }

                    }
                }
            }
            return returnList;
        }
    }
}
